package me.zhengjie.modules.customer.rest;

import io.swagger.annotations.Api;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.customer.domain.TCustomerContract;
import me.zhengjie.modules.customer.service.TCustomerContractService;
import me.zhengjie.modules.customer.service.dto.TCustomerContractQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @author xuyukun
* @date 2019-07-21
*/
@RestController
@Api(tags = "客户合同管理")
@RequestMapping("api")
public class TCustomerContractController {

    @Autowired
    private TCustomerContractService tCustomerContractService;

    @Log("查询TCustomerContract")
    @GetMapping(value = "/tCustomerContract")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERCONTRACT_ALL','TCUSTOMERCONTRACT_SELECT')")
    public ResponseEntity getTCustomerContracts(TCustomerContractQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tCustomerContractService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TCustomerContract")
    @PostMapping(value = "/tCustomerContract")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERCONTRACT_ALL','TCUSTOMERCONTRACT_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TCustomerContract resources){
        return new ResponseEntity(tCustomerContractService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TCustomerContract")
    @PutMapping(value = "/tCustomerContract")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERCONTRACT_ALL','TCUSTOMERCONTRACT_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TCustomerContract resources){
        tCustomerContractService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TCustomerContract")
    @DeleteMapping(value = "/tCustomerContract/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TCUSTOMERCONTRACT_ALL','TCUSTOMERCONTRACT_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        try {
            tCustomerContractService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        }catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }
}